package boc.sx.gateway.core;


import boc.sx.gateway.core.strategy.LBWay;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.Yaml;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * @author zmx
 * @date 2022/8/8 17:13
 */
@Component
public class ServiceUrlLoader {

//    @Resource
//    private JdbcTemplate jdbcTemplate;

    private static Map<String,ServiceNode> stringServiceNodeMap =null;


    @Bean("serverURLInfo")
    public Map<String,ServiceNode> getServerInfo(){
        if(stringServiceNodeMap ==null){
            load();
        }
        return stringServiceNodeMap;
    }

    public final String URL_PROPERTIES = "application.yml";

    public void load(){
        stringServiceNodeMap = new HashMap<>();
        ServiceNode serviceNode = new ServiceNode("HS", "S2", "http", "json", new String[]{"localhost:8081"}, "/getAll", new Integer[]{1}, LBWay.getLBWay(1));
        stringServiceNodeMap.put("HS"+"-"+"S2",serviceNode);
        ServiceNode serviceNode2 = new ServiceNode("SS", "S1", "simple", "json", new String[]{"127.0.0.1:8084"}, "/getAll", new Integer[]{1}, LBWay.getLBWay(1));
        stringServiceNodeMap.put("SS"+"-"+"S1",serviceNode2);
//        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from service_mapping");
//        for(Map<String, Object> map:maps){
//            String sys_id = (String)map.get("SYS_ID");
//            String ser_id = (String) map.get("SER_ID");
//            String urlstr = (String) map.get("URL");
//            String dataType = (String) map.get("DATA_TYPE");
//            String protocol = (String) map.get("PROTOCOL");
//            String path = (String) map.get("PATH");
//            String[] split = urlstr.split(",");
//            String urls[] = new String[split.length];
//            Integer weight[] = new Integer[split.length];
//            Arrays.fill(weight,1);
//            for (int i=0;i<split.length;i++){
//                urls[i] = split[i];
//                if(split[i].contains("-")){
//                    String[] split1 = split[i].split("-");
//                    urls[i] = split1[0];
//                    if(StringUtils.isNumeric(split1[1])){
//                        weight[i] = Integer.parseInt(split1[1]);
//                    }
//                }
//            }
//            Integer lb_way = (Integer) map.get("LB_WAY");
//            ServiceNode serviceNode = new ServiceNode(sys_id, ser_id, protocol,dataType,urls,path,weight, LBWay.getLBWay(lb_way));
//            stringServiceNodeMap.put(sys_id+"-"+ser_id,serviceNode);
//        }
    }

    public void loadFromYaml(){
//        Yaml yaml = new Yaml();
//        InputStream resourceAsStream = ServiceUrlLoader.class.getClassLoader().getResourceAsStream(URL_PROPERTIES);
//        Map<String,Object> obj = yaml.load(resourceAsStream);
//        Map<String, Object> map = (Map<String, Object>)obj.get("service");;
//        map.forEach((systemName,v)->{
//            if (!StringUtils.isEmpty(systemName)) {
//                Map<String, Object> sub = (Map<String, Object>)v;
//                sub.forEach( (serviceName,value)->{
//                    Map<String, Object> property = (Map<String, Object>)value;
//                    if(!property.containsKey("ip")){
//                        logger.log(Level.SEVERE,serviceName+ " 下的配置出错: 未配置IP信息");
//                        return;
//                    }
//                    if(!property.containsKey("data")){
//                        logger.log(Level.SEVERE,serviceName+ " 下的配置出错: 未指定期待数据格式");
//                        return;
//                    }
//                    String ip = (String)property.get("ip");
//                    Integer lbWay = (Integer)property.get("lb");
//                    String[] split = ip.split(",");
//                    Integer weight[] = new Integer[split.length];
//                    ServiceNode serviceNode = new ServiceNode(systemName, serviceName, "http","xml",split,weight, LBWay.getLBWay(lbWay));
//                    stringServiceNodeMap.put(serviceName + "-" + serviceName , serviceNode);
//                });
//
//            }else{
//                logger.log(Level.WARNING,"未配置服务信息,可能发生无法预料的错误");
//            }
//        });

    }

    public static ServiceNode findServiceNode(String s){
        return stringServiceNodeMap.get(s);
    }
}
